/**
 */

package com.bei_en.common.web;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.log4j.Logger;

import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;

/**
 * 重复登录踢掉
 * 
 * @author Guocg
 *
 */
public class MySessionListener implements HttpSessionAttributeListener {

	private Logger log = Logger.getLogger(this.getClass());

	/*
	 * @author Guocg
	 */
	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {

		HttpSession session = se.getSession();
		log.info("attributeAdded:" + se.getName() + "," + se.getValue());
		if (se.getName().equals(Consts.LOGIN_USER)) {
			UsersVo vo = (UsersVo) se.getValue();
			log.info("attributeAdded:" + session.getId() + "," + se.getName()
					+ "," + vo.getUserId());
			MySessionContext.addSession(vo.getUserId(), session);
		}

	}

	/*
	 * @author Guocg
	 */
	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {

		HttpSession session = se.getSession();
		log.info("attributeRemoved:" + session.getId() + "," + se.getName()
				+ "," + se.getValue());
		if (se.getName().equals(Consts.LOGIN_USER)) {
			UsersVo vo = (UsersVo) se.getValue();
			log.info("attributeRemoved:" + session.getId() + "," + se.getName()
					+ "," + vo.getUserId());
			MySessionContext.removeSession(vo.getUserId());
		}

	}

	/*
	 * @author Guocg
	 */
	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {

		HttpSession session = se.getSession();
		log.info("attributeReplaced:" + session.getId() + se.getName() + ","
				+ se.getValue());
		if (se.getName().equals(Consts.LOGIN_USER)) {
			UsersVo vo = (UsersVo) se.getValue();
			log.info("attributeReplaced:" + session.getId() + ","
					+ se.getName() + "," + vo.getUserId());
			MySessionContext.addSession(vo.getUserId(), session);
		}
	}

}